////////////////////////////////////////////////////////////////////////////////
// Copyright 2010 Michael Schmalle - Teoti Graphix, LLC
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0 
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and 
// limitations under the License
// 
// Author: Michael Schmalle, Principal Architect
// mschmalle at teotigraphix dot com
////////////////////////////////////////////////////////////////////////////////

package org.teotigraphix.swizplugin.bundles
{

import org.swizframework.core.Bean;
import org.swizframework.core.IBeanFactory;
import org.swizframework.core.ISwiz;

/**
 * A simple bundle storage implementation that reads all BundleData instances
 * from the beanFactory provided.
 * 
 * <p>This class can be extended to alter the reading of bundles. The only
 * requirement is that <code>#readBundleDatas()</code> method returns an 
 * array of <code>BundleData</code> instances.</p>
 * 
 * @author Michael Schmalle
 * @copyright Teoti Graphix, LLC
 * @productversion 1.0
 */
public class BundleStorage
{
	//--------------------------------------------------------------------------
	//
	//  Private :: Variables
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	private var _adaptor:IBundleServiceAdaptor;
	
	/**
	 * @private
	 */
	private var _beanFactory:IBeanFactory;
	
	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function BundleStorage()
	{
		super();
	}
	
	//--------------------------------------------------------------------------
	//
	//  Public :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	public function initialize(adaptor:IBundleServiceAdaptor,
							   beanFactory:IBeanFactory):void
	{
		_adaptor = adaptor;
		_beanFactory = beanFactory;
	}
	
	/**
	 * @private
	 */
	public function getInstalledBundles():Array // of BundleData
	{	
		return readBundleDatas();
	}
	
	//--------------------------------------------------------------------------
	//
	//  Private :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	protected function readBundleDatas():Array // of BundleData
	{
		var result:Array = [];
		
		// in an AIR impl, we would loop through all the directories in the plugins
		// in an MXML impl we would loop through all the Beans
		
		// 1) Add all inital bundles to service
		var beans:Array = _beanFactory.beans;
		var len:int = beans.length;
		for (var i:int = 0; i < len; i++)
		{
			var bean:Bean = beans[i] as Bean;
			if (bean.source is BundleData)
			{
				result.push(bean.source);
			}
		}
		
		return result;
	}
}
}